import request from '@/utils/request'
import { isEmail } from '@/utils/common'
import { wxAppid } from '@/config'

// 登录方法
export function login(username, password, code, uuid) {
  const data = {
    password,
    code,
    uuid
  }
  if (isEmail(username)) {
    data.email = username
  } else {
    data.phonenumber = username
  }
  return request({
    'url': '/tai-auth/login',
    headers: {
      isToken: false
    },
    'method': 'post',
    'data': data
  })
}

// 小程序授权登录方法
export function wechatLogin(openid) {
  const data = {
    openid
  }
  return request({
    'url': '/tai-auth/login-by-wechat',
    headers: {
      isToken: false
    },
    'method': 'post',
    'data': data
  })
}

// 注册方法
export function register(data) {
  return request({
    url: '/register',
    headers: {
      isToken: false
    },
    method: 'post',
    data: data
  })
}

// 获取用户详细信息
export function getInfo() {
  return request({
    'url': '/wechat/member/info',
    'method': 'post'
  })
}

// 退出方法
export function logout() {
  return request({
    'url': '/auth/logout',
    'method': 'delete'
  })
}

// 获取验证码
export function getCodeImg() {
  return request({
    'url': '/code',
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}


// 微信手机号登录
export function loginByWechatPhone(jsCode, openId) {
  const data = {
    appid: wxAppid,
    jsCode,
    openId
  }
  return request({
    'url': `/tai-auth/login-by-wechat-phone`,
    headers: {
      isToken: false
    },
    method: 'post',
	  data
  })
}

// 登录凭证
export function greet(jsCode) {
	const data = {
		appid: wxAppid,
		jsCode
	}
  return request({
    'url': `/wechat/auth/${wxAppid}/greet`,
    headers: {
      isToken: false
    },
    method: 'post',
	data,
  })
}


// 验证是否绑定微信
export function checkBindWechat(openid) {
	const data = {
		openid
	}
  return request({
    'url': `/tai-auth/check-bind-wechat`,
    method: 'post',
	data,
  })
}

// 绑定微信号
export function bindWechat(openid) {
	const data = {
		openid
	}
  return request({
    'url': `/wechat/member/bind-wechat`,
    method: 'post',
	data,
  })
}

// 解绑微信号
export function unbindWechat() {
  return request({
    'url': `/wechat/member/unbind-wechat`,
    method: 'post',
  })
}

/**
 * 人脸身份认证
 * @param {*} data 
 * @param {string} data.name 姓名
 * @param {string} data.idCardNumber 身份证号码
 * @param {string} data.picUrl 人脸图片
 * @returns 
 */
export function faceVerify(data) {
  return request({
    url: '/wechat/member/face-verify',
    method: 'post',
    data: data
  })
}

/**
 * 修改密码
 * @param {Object} data
 */
export function updatePwd(data) {
  return request({
    url: '/wechat/member/update-password',
    method: 'post',
    data: data
  })
}

/**
 * 修改头像与昵称
 * @param {*} data 
 * @param {string} data.nickName 昵称
 * @param {string} data.avatar 头像
 * @returns 
 */
export function uploadProfile(data) {
  return request({
    url: '/wechat/member/update-profile',
    method: 'post',
    data: data
  })
}